package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.NewscategoryDaoImpl;
import com.ysd.dao.impl.NewsdetailsDaoImpl;
import com.ysd.entity.Newscategory;
import com.ysd.entity.Newsdetails;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;



public class AdminNewsCategoryServlet extends BaseServlet {
	/**
	 *分页 查询所有新闻分类
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=getNewsCategory
	 *
	 */
	public void getNewsCategory(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		NewscategoryDaoImpl newdao = new NewscategoryDaoImpl();// 创建实现类对象
		int page=Integer.parseInt(request.getParameter("page"));
		int size=Integer.parseInt(request.getParameter("size"));
		List<Newscategory> newList = newdao.getNewscategoryByPage(page, size);// 分页查到的所有新闻分类集合

		response.setCharacterEncoding("UTF-8");// 设置编码格式
		PrintWriter out = response.getWriter();// 回信者 out
		Gson gson = new Gson();// 创建Gson对象
		String res = gson.toJson(newList);// 将查询到的集合转成gson格式
		out.print(res);// 打印

		out.flush();
		out.close();
	}
	/**
	 * 查询所有新闻分类的个数
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=getAllNewsCategory
	 *
	 */
	public void getAllNewsCategory(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		NewscategoryDaoImpl newdao = new NewscategoryDaoImpl();// 创建实现类对象
		
		List<Newscategory> newList = newdao.getNewscategory();// 查到的所有新闻分类集合

		response.setCharacterEncoding("UTF-8");// 设置编码格式
		PrintWriter out = response.getWriter();// 回信者 out
		Gson gson = new Gson();// 创建Gson对象
		String res = gson.toJson(newList.size());// 将查询到的集合转成gson格式
		out.print(res);// 打印

		out.flush();
		out.close();
	}
	
	/**
	 * 根据id查询新闻分类
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=getNewsCategoryById
	 *
	 */
	public void getNewsCategoryById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");// 设置编码格式
		PrintWriter out = response.getWriter();// 回信者 out
		NewscategoryDaoImpl newdao = new NewscategoryDaoImpl();// 创建实现类对象
		int nid = Integer.parseInt(request.getParameter("newsCategoryId"));//接受前台参数并转化为int类型
		Newscategory ne=newdao.getNewscategoryById(nid);
		Gson gson=new Gson();
		String res=gson.toJson(ne);//把新闻呢分类转为json字符串
		out.print(res);
		out.flush();
		out.close();
	}
	
	/**
	 * 根据id修改新闻分类
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=updateNewsCategoryById
	 *
	 */
	public void updateNewsCategoryById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");// 设置编码格式
		PrintWriter out = response.getWriter();// 回信者 out
		NewscategoryDaoImpl newdao = new NewscategoryDaoImpl();// 创建实现类对象
		int newsCategoryId=Integer.parseInt(request.getParameter("newsCategoryId"));//接受前台参数
		String newsCategoryName=request.getParameter("newsCategoryName");
		List<Newscategory> newList=newdao.getNewscategoryByName(newsCategoryName);
		if (newList.size()==0) {
			Newscategory ne=new Newscategory(newsCategoryId, newsCategoryName);//创建新闻分类对象
			int n=newdao.updateNewsCategory(ne);
			if (n > 0) {// 修改成功
				out.print(Result.toClient(true, "修改成功", ""));
			} else {// 失败
				out.print(Result.toClient(false, "修改失败", ""));
			}
		} else {
			out.print(Result.toClient(false, "分类名已存在", ""));
		}
		
		
		out.flush();
		out.close();
	}

	/**
	 * 1 添加新闻分类
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=addNewsCategory
	 *
	 */

	public void addNewsCategory(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置编码格式
		PrintWriter out = response.getWriter();// out对象
		NewscategoryDaoImpl newdao = new NewscategoryDaoImpl();// 创建实现类对象

		String newsCategoryName = request.getParameter("newsCateName");
		if (newsCategoryName != null && !newsCategoryName.equals("")) {
			List<Newscategory> cateList = newdao.getNewscategoryByName(newsCategoryName);
			if (cateList.size() == 0) {// ==0就是没有查到同名得,可以添加
				Newscategory ne = new Newscategory(0, newsCategoryName);// 创建新闻分类对象
				int n = newdao.addNewsCategory(ne);
				if (n > 0) {// 添加成功
					out.print(Result.toClient(true, "添加成功", ""));
				} else {// 失败
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "分类名称已存在", ""));
			}
		}

		out.flush();
		out.close();
	}

	/**
	 * 根据id删除新闻分类
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=delNewsCategory
	 *
	 */
	public void delNewsCategory(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int nid = Integer.parseInt(request.getParameter("newsCategoryId"));// 接受前台传来参数
		NewscategoryDaoImpl newdao = new NewscategoryDaoImpl();// 创建实现类对象
		int n = newdao.deleteNewsCategoryById(nid);// 调用删除方法
		response.setCharacterEncoding("UTF-8");// 设置响应格式
		PrintWriter out = response.getWriter();// 回信者 out
		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}

		out.flush();
		out.close();
	}
	/**
	 * 根据id批量删除新闻分类
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=delNewsCategoryByIds
	 *
	 */
	public void delNewsCategoryByIds(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应格式
		PrintWriter out = response.getWriter();// 回信者 out
		String ids=request.getParameter("ids");
		NewscategoryDaoImpl newdao = new NewscategoryDaoImpl();// 创建实现类对象
		int n = newdao.delNewsCategoryByIds(ids);// 调用删除方法
		
		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}

		out.flush();
		out.close();
	}

	/**
	 * 查询所有新闻详情 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=getNewsDetails
	 *
	 */
	public void getNewsDetails(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int newsId = 0;
		if (request.getParameter("newsId") != null && !request.getParameter("newsId").equals("")) {
			newsId = Integer.parseInt(request.getParameter("newsId"));// 接受前台传来参数
		}
		String newsTitle = request.getParameter("newsTitle");
		String nwesContent = request.getParameter("nwesContent");
		int newsCategoryId = 0;
		if (request.getParameter("newsCategoryId") != null && !request.getParameter("newsCategoryId").equals("")) {
			newsCategoryId = Integer.parseInt(request.getParameter("newsCategoryId"));
		}

		String newsCategoryName = request.getParameter("newsCategoryName");

		// 根据前台接受得参数创建新闻对象
		Newsdetails n = new Newsdetails(newsId, newsTitle, nwesContent, newsCategoryId, newsCategoryName);
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		NewsdetailsDaoImpl newsDao = new NewsdetailsDaoImpl();// 创建实现类对象
		List<Newsdetails> newsDaoList = newsDao.getNewsDetailsByPage(n, page, size);// 查到的所有新闻详情集合 调用实现类中多条件查询得方法

		response.setCharacterEncoding("UTF-8");// 设置响应格式
		PrintWriter out = response.getWriter();// 回信者 out
		Gson gson = new Gson();// 创建Gson对象
		String res = gson.toJson(newsDaoList);// 将查询到的集合转成gson格式
		out.print(res);// 打印

		out.flush();
		out.close();
	}
	/**
	 * 查询所有新闻详情个数 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=getAllNewsDetails
	 *
	 */
	public void getAllNewsDetails(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int newsId = 0;
		if (request.getParameter("newsId") != null && !request.getParameter("newsId").equals("")) {
			newsId = Integer.parseInt(request.getParameter("newsId"));// 接受前台传来参数
		}
		String newsTitle = request.getParameter("newsTitle");
		String nwesContent = request.getParameter("nwesContent");
		int newsCategoryId = 0;
		if (request.getParameter("newsCategoryId") != null && !request.getParameter("newsCategoryId").equals("")) {
			newsCategoryId = Integer.parseInt(request.getParameter("newsCategoryId"));
		}

		String newsCategoryName = request.getParameter("newsCategoryName");

		// 根据前台接受得参数创建新闻对象
		Newsdetails n = new Newsdetails(newsId, newsTitle, nwesContent, newsCategoryId, newsCategoryName);
		
		NewsdetailsDaoImpl newsDao = new NewsdetailsDaoImpl();// 创建实现类对象
		List<Newsdetails> newsDaoList = newsDao.getNewsDetailsByWhere(n);// 查到的所有新闻详情集合 调用实现类中多条件查询得方法

		response.setCharacterEncoding("UTF-8");// 设置响应格式
		PrintWriter out = response.getWriter();// 回信者 out
		Gson gson = new Gson();// 创建Gson对象
		String res = gson.toJson(newsDaoList.size());// 将查询到的集合转成gson格式
		out.print(res);// 打印

		out.flush();
		out.close();
	}

	/**
	 * 根据id删除新闻详情
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=delNewsDetails
	 *
	 */
	public void delNewsDetails(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int nid = Integer.parseInt(request.getParameter("newsId"));// 接受前台传来参数
		NewsdetailsDaoImpl newsDao = new NewsdetailsDaoImpl();// 创建实现类对象
		int n = newsDao.deleteNewsDetailsById(nid);
		response.setCharacterEncoding("UTF-8");// 设置响应格式
		PrintWriter out = response.getWriter();// 回信者 out
		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}

		out.flush();
		out.close();
	}
	/**
	 * 根据id批量删除新闻详情
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=delNewsDetailsByIds
	 *
	 */
	public void delNewsDetailsByIds(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应格式
		PrintWriter out = response.getWriter();// 回信者 out
		String ids=request.getParameter("ids");// 接受前台传来参数
		NewsdetailsDaoImpl newsDao = new NewsdetailsDaoImpl();// 创建实现类对象
		int n = newsDao.delNewsdetailsByIds(ids);
		
		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}

		out.flush();
		out.close();
	}

	/**
	 * 添加新闻详情 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=addDetails
	 *
	 */

	public void addDetails(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置编码格式
		PrintWriter out = response.getWriter();// out对象
		NewsdetailsDaoImpl newsdao = new NewsdetailsDaoImpl();

		String nwesContent = request.getParameter("nwesContent");
		int newsCategoryId = 0;
		if (request.getParameter("newsCategoryId") != null && !request.getParameter("newsCategoryId").equals("")) {
			newsCategoryId = Integer.parseInt(request.getParameter("newsCategoryId")); // NumberFormatException数字格式化异常
		}
		String newsTitle = request.getParameter("newsTitle");
		if (newsTitle != null && !newsTitle.equals("")) {
			List<Newsdetails> nt = newsdao.getNewsdetailsByTitle(newsTitle);
			if (nt.size() == 0) {// 没有查到可以添加
				Newsdetails ns = new Newsdetails(0, newsTitle, nwesContent, newsCategoryId);

				int n = newsdao.addNewsDetails(ns);
				if (n > 0) {// 添加成功
					out.print(Result.toClient(true, "添加成功", ""));
				} else {// 失败
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "标题已存在", ""));
			}
		}

		out.flush();
		out.close();
	}
	/**
	 * 根据id查询新闻详情
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=getNewsDetailsById
	 *数据回显
	 */
	public void getNewsDetailsById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应格式
		PrintWriter out = response.getWriter();// 回信者 out
		
		int id=Integer.parseInt(request.getParameter("newsId"));
		NewsdetailsDaoImpl newsDao = new NewsdetailsDaoImpl();// 创建实现类对象
		Newsdetails news=newsDao.getNewsDetailsById(id);
		Gson gson=new Gson();
		String res=gson.toJson(news);
		out.print(res);

		out.flush();
		out.close();
	}
	/**
	 * 根据id修改新闻详情
	 * 访问地址:localhost:8080/meizizi/adminNewsCategory?reqName=updateNewsDetailsById
	 *数据回显
	 */
	public void updateNewsDetailsById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应格式
		PrintWriter out = response.getWriter();// 回信者 out
		
		int newsId=Integer.parseInt(request.getParameter("newsId"));
		String newsTitle=request.getParameter("newsTitle");
		String nwesContent=request.getParameter("nwesContent");
		int newsCategoryId=Integer.parseInt(request.getParameter("newsCategoryId"));
		NewsdetailsDaoImpl newsDao = new NewsdetailsDaoImpl();// 创建实现类对象
		List<Newsdetails>newsList=newsDao.getNewsdetailsByTitle(newsTitle);
		if (newsList.size()==0) {//没有查到同标题的,可以修改
			Newsdetails ns=new Newsdetails(newsId, newsTitle, nwesContent, newsCategoryId);
			int n=newsDao.updateNewsDetails(ns);
			if (n>0) {//修改成功
				out.print(Result.toClient(true, "修改成功", ""));
			} else {//失败
				out.print(Result.toClient(false, "修改失败", ""));
			}

		} else {//!=0 就是查到同标题的了不能修改
			out.print(Result.toClient(false, "标题重复", ""));
		}
		
		out.flush();
		out.close();
	}
}
